/**
 * Created by Administrator on 2017/9/21.
 */
angular.module('module.saleStatistics')
    .controller('upperVolumeController', upperVolumeController);
upperVolumeController.$inject = ['$scope', '$localStorage', 'shopSaleList', '$state'];
function upperVolumeController($scope, $localStorage, shopSaleList, $state) {
    var vm = this;
    var myDate = new Date();
    vm.option = option;
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框
    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }
    var shopid = $localStorage[environmental + "shopid"];

    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    if (shopSaleList == 'undefined') {
        vm.uplist = [];
    } else {
        vm.uplist = shopSaleList;
    }
    vm.type = 3;
    vm.img = img2;
    var mystatime = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate() - 6, 0, 0, 0);
    vm.tur_statime = getstart(mystatime, 0);
    vm.tur_endtime = getstart(new Date(), 1);

    var timelist = {
        statime: Date.parse(mystatime),
        endtime: Date.parse(new Date()),
    };

    getecharts(timelist.statime, timelist.endtime);

    $('#starttime').datetimepicker({
        format: "yyyy-MM-dd",
        autoclose: true,
        todayBtn: true,
        pickerPosition: "bottom-right",
        minView: 2,
        endDate: new Date(),
    }).on('changeDate', function (ev) {
        timelist.statime = jonsr(getstart(ev.date, 0));
        if (timelist.endtime == null || timelist.endtime == "") {
            $scope.$apply(function () {
                vm.tur_statime = getstart(ev.date, 0);
            })
        } else {
            if (timelist.statime > timelist.endtime) {
                vm.messageVisible = true;
                vm.message = "请输入正确的时间格式！";
                return;
            }
            $scope.$apply(function () {
                vm.tur_statime = getstart(ev.date, 0);
                getecharts(timelist.statime, timelist.endtime);
            })


        }
    });

    $('#endtime').datetimepicker({
        format: "yyyy-MM-dd",
        autoclose: true,
        todayBtn: true,
        pickerPosition: "bottom-right",
        minView: 2,
        endDate: new Date(),
    }).on('changeDate', function (ev) {
        timelist.endtime = jonsr(getstart(ev.date, 1));
        if (timelist.endtime == null || timelist.endtime == "") {
            $scope.$apply(function () {
                vm.tur_endtime = getstart(ev.date, 1);
            })
        } else {
            if (timelist.statime > timelist.endtime) {
                vm.messageVisible = true;
                vm.message = "请输入正确的时间格式！";
                return;
            }
            $scope.$apply(function () {
                vm.tur_endtime = getstart(ev.date, 1);
                getecharts(timelist.statime, timelist.endtime);
            })


        }
    });

    function getecharts(starttime, endtime) {
        vm.tableNum = [];
        var listecharts = [];
        var henglistecharts = [];
        var zonglistecharts = [];
        for (var i = 0; i < vm.uplist.length; i++) {
            vm.uplist[i].time = jonsr(echartime(vm.uplist[i].date, 0));
            if (vm.uplist[i].time > starttime && vm.uplist[i].time < endtime) {
                listecharts.push(vm.uplist[i]);
                henglistecharts.push(vm.uplist[i].time);
                zonglistecharts.push(vm.uplist[i].shopnumSum);
                var data = {};
                data['time'] = echartime(vm.uplist[i].date, 1);
                data['num'] = vm.uplist[i].shopnumSum;
                vm.tableNum.push(data);
            }
        }
        var option = vm.option(henglistecharts, zonglistecharts);
        var diagram = echarts.init(document.getElementById('tubiao'));
        diagram.setOption(option);
    }


    function img2(type) {
        vm.type = type;
        switch (type) {
            case 2:
                timelist.statime = new Date(new Date().getFullYear(), new Date().getMonth() - 1, new Date().getDate(), 0, 0, 0).getTime(); // 1涓湀鍓嶇殑鏃堕棿鎴�
                timelist.endtime = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 23, 59, 59).getTime(); // 浠婂ぉ鐨勬椂闂存埑
                getecharts(timelist.statime, timelist.endtime);
                break;
            case 3:
                timelist.statime = Date.parse(mystatime);
                timelist.endtime = Date.parse(new Date()); // 浠婂ぉ鐨勬椂闂存埑
                getecharts(timelist.statime, timelist.endtime);
                break;
            default :
                timelist.statime = Date.parse(mystatime);
                timelist.endtime = Date.parse(new Date()); // 浠婂ぉ鐨勬椂闂存埑
                getecharts(timelist.statime, timelist.endtime);
        }
    }

    function option(data, dataAxis) {
        return {
            tooltip: {
                trigger: 'axis'
            },
            grid: {
                x: 25,
                y: 15,
                x2: 25,
                y2: 15,
                borderWidth: 1

            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: data,
                axisLine: {
                    show: false
                },
                axisTick: {
                    show: false
                },
                splitLine: {
                    show: true,
                    lineStyle: {
                        color: '#5fe1d9'
                    }
                },
                axisLabel: {
                    show: false
                }
            },
            yAxis: {
                type: 'value',
                axisLine: {
                    show: false

                },
                axisTick: {
                    show: false
                }, splitLine: {
                    show: false
                },
                axisLabel: {
                    show: false
                }
            },
            series: [
                {
                    type: 'line',
                    symbol: 'none',
                    smooth: 'true',
                    itemStyle: {
                        normal: {
                            color: 'white',
                            lineStyle: {
                                normal: {
                                    width: 10,
                                    color: 'white'
                                }
                            },
                        }
                    },
                    data: dataAxis,
                },

            ]
        };
    }
}